//
//  ForgetSetPwdViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/6/29.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "ForgetSetPwdViewController.h"
#import "ForgetPwdViewModel.h"

@interface ForgetSetPwdViewController ()
@property (nonatomic,copy) NSString * yanzhengmeString;
@property (nonatomic,copy) NSString * strTime;
@property (nonatomic,copy) NSString * strPhone;

@property (weak, nonatomic) IBOutlet UITextField *setPwd;
@property (weak, nonatomic) IBOutlet UITextField *setAgainPwd;
@property (weak, nonatomic) IBOutlet UIButton *setCompleteClick;

@property (nonatomic,strong) ForgetPwdViewModel * forgetViewModel;

@end

@implementation ForgetSetPwdViewController

- (id)initWithCode:(NSString *) strCode time:(NSString *) strTime phone:(NSString *)strPhone
{
    self = [super init];
    if (self) {
        self.yanzhengmeString = strCode;
        self.strTime = strTime;
        self.strPhone = strPhone;
    }
    return self;
}
-(ForgetPwdViewModel *) forgetViewModel{
    if (!_forgetViewModel) {
        _forgetViewModel = [[ForgetPwdViewModel alloc] init];
    }
    return _forgetViewModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationItem setTitle:@"忘记密码"];
    [self.view setBackgroundColor:DEF_COLOR_F0F0F0];
    
    [[self.setCompleteClick rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        if ([self ifelse]) {
            
            [self forgetNetWork];
        }
    }];
}

-(void) forgetNetWork{

    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.forgetViewModel forgetPwdRequestParameter:self.strPhone password:self.setPwd.text code:self.yanzhengmeString strTime:self.strTime] subscribeNext:^(id result) {
       
        if([result[@"errorcode"] integerValue] == 0){
            
            
            [MXUtility showTips:@"找回成功,前往登录～"];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }
        
        
        [MXUtility hideMBProgress:self.view];
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
    }];
    
}


-(BOOL) ifelse{

    if ([MXUtility NSNULLString:self.setPwd.text]) {
        [MXUtility showTips:@"密码不能为空"];
        return false;
    }
    if (![MXUtility CheckInput:self.setPwd.text]) {
        [MXUtility showTips:@"请输入正确的密码"];
        return false;
    }
    if ([MXUtility NSNULLString:self.setAgainPwd.text]) {
        [MXUtility showTips:@"确认密码不能为空"];
        return false;
    }
    if ((![self.setPwd.text isEqualToString:self.setAgainPwd.text])) {
        [MXUtility showTips:@"两次输入密码不一致"];
        return false;
    }
    return true;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
